// @charset "utf-8";
/**
 * 短期分析-dqfx
 */
define(function (require, exports, module) {
	var dqfxModel = require('./dqfxmodel');
	var dqfxChart = require('./dqfxchart');
	var dqfxView = {

		param : {
			zf : '1',
			df : '4',
			begin : '',
			end : ''
		},
		init : function () {
			var self = this;
			var tradingDay = commonMethod.getTradingDay();
			if (tradingDay == 0) {
				var getTradeDayTimer = setTimeout(function () {
					self.init();
				}, 200);
				return;
			} else {
				clearTimeout(getTradeDayTimer);
			}

			interactions.datePicker(tradingDay, 'dqfx', function (date) {

			}, function (startDate, endDate) {
				self.param.begin = startDate;
				self.param.end = endDate;
				self.requestData();
			});
			self.hoverChart();
		},
		autoRefresh : function (isIn) {
			var self = this;
			var isToday = self.param.end == commonMethod.serverTime();
			// var isTrade = commonMethod.thsQuote.isTradeTime('1A0001');
			var isTrade = commonMethod.judgeIntime(['09:00', '16:00']);
			if ($('.tab-active').index() == 1 && ((isToday && isTrade) || isIn)) {
				self.requestData();
			}
		},
		requestData : function () {
			var self = this;
			var beginDate = $('#view_input_dqfxdate_start').val().replace(/-/g,'');
			var endDate = $('#view_input_dqfxdate_end').val().replace(/-/g,'');
			self.param.begin = self.param.begin == '' ? beginDate : self.param.begin;
			self.param.end = self.param.end == '' ? endDate : self.param.end;
			dqfxModel.request(self.param, function (result) {
				self.zdfResult = result;

				if (!$.isEmptyObject(result)) {
					var timeArr1 = [];
					$.each(result, function (i, val) {
						timeArr1.push(i);
					});
					self.requestShData(timeArr1);
				} else {

				}
			});
		},
		zdfResult : {},
		xAxisDate : [],
		zfData1 : [],
		zfData2 : [],
		zfData3 : [],
		dfData1 : [],
		dfData2 : [],
		dfData3 : [],
		shData : [],
		newZfData : '--',
		newDfData : '--',
		newShData : '--',
		requestShData : function (timeArr1) {
			var self = this;
			var stocks = ['1A0001'];
			var quoteType = ['close'];
			var requestParam = {
				code: stocks,
				period: 'day',
				validbegin: -100,
				validend: 0,
				type: quoteType,
				onready: function() {
					var param = {
						code: stocks,
						type: quoteType,
						mode: 'after',
						period: 'day',
						time: {
							begin: self.param.begin,
							end: self.param.end
						}
					};
					var shResultStr = commonMethod.thsQuote.getData2(param);
					// alert(shResultStr);
					var shResultObj = JSON.parse(shResultStr);
					var data = shResultObj[stocks];
					var shData = [];

					var timeArr2 = [];
					$.each(data, function (i, val) {
						timeArr2.push(i);
					});

					var timeArr = commonMethod.arrayIntersection(timeArr1, timeArr2);
					if (timeArr.length != 0) {
						var useZdfResult = {},
							useShResult = {};
						$.each(timeArr, function (j, time) {
							useZdfResult[time] = self.zdfResult[time];
							useShResult[time] = data[time];
						});
						$.each(useShResult, function (i, val) {
							var shdata = parseFloat(parseFloat(val.close).toFixed(2));
							shData.push(shdata);
						});

						self.shData = shData;
						self.zdfResult = useZdfResult;
						// 取相同日期的dqfx数据
						var zfData1 = [],
							zfData2 = [],
							zfData3 = [],
							dfData1 = [],
							dfData2 = [],
							dfData3 = [];
						var xAxisDate = [];
						$.each(useZdfResult, function (i, val) {
							xAxisDate.push(i.substr(4, 2) + '-' + i.substr(6));
							zfData1.push(parseInt(val['data']['1']));
							zfData2.push(parseInt(val['data']['2']));
							zfData3.push(parseInt(val['data']['3']));
							dfData1.push(parseInt(val['data']['4']));
							dfData2.push(parseInt(val['data']['5']));
							dfData3.push(parseInt(val['data']['6']));
						});
						self.xAxisDate = xAxisDate;
						self.zfData1 = zfData1;
						self.zfData2 = zfData2;
						self.zfData3 = zfData3;
						self.dfData1 = dfData1;
						self.dfData2 = dfData2;
						self.dfData3 = dfData3;

						self.analyticalData();
					} else {

					}

				}
			};
			commonMethod.thsQuote.request(requestParam);
		},

		zdftype : {
			'1' : '3日涨幅大于10%',
			'2' : '3日涨幅大于20%',
			'3' : '3日涨幅大于30%',
			'4' : '3日跌幅大于10%',
			'5' : '3日跌幅大于20%',
			'6' : '3日跌幅大于30%'
		},
		zfLinkText : '',
		dfLinkText : '',
		analyticalData : function () {
			var self = this;

			self.zfLinkText = self.zdftype[self.param['zf']];
			self.dfLinkText = self.zdftype[self.param['df']];
			$('.view_span_zfselcur').text(self.zfLinkText);
			$('.view_span_dfselcur').text(self.dfLinkText);

			var zfdata,
				dfdata;
			if (self.param.zf == '1') {
				zfdata = self.zfData1;
			}
			if (self.param.zf == '2') {
				zfdata = self.zfData2;
			}
			if (self.param.zf == '3') {
				zfdata = self.zfData3;
			}
			if (self.param.df == '4') {
				dfdata = self.dfData1;
			}
			if (self.param.df == '5') {
				dfdata = self.dfData2;
			}
			if (self.param.df == '6') {
				dfdata = self.dfData3;
			}
			self.newZfData = zfdata[zfdata.length - 1];
			self.newDfData = dfdata[dfdata.length - 1];
			self.newShData = self.shData[self.shData.length - 1];

			$('.view_i_dqfxzf').text(self.newZfData);
			$('.view_i_dqfxdf').text(self.newDfData);

			var chartData = {
				xAxisDate : self.xAxisDate,

				zfData : zfdata,
				dfData : dfdata,

				shData : self.shData,
				newZfData : self.newZfData,
				newDfData : self.newDfData,
				newShData : self.newShData
			};
			dqfxChart.render(chartData, self.param);
		},
		hoverChart : function () {
			var self = this;
			$('#view_div_dqfxchart').unbind('mouseleave').mouseleave(function() {
				$('.view_i_dqfxzf').text(self.newZfData);
				$('.view_i_dqfxdf').text(self.newDfData);
			});

			$('.dqfx-legend-sh').on('click', function () {
				if (dqfxChart.dqfxHighcahrts != null) {
					if (dqfxChart.dqfxHighcahrts.series[2].visible) {
						dqfxChart.dqfxHighcahrts.series[2].hide();
					} else {
						dqfxChart.dqfxHighcahrts.series[2].show();
					}
				}

			});

			// 跳转链接
			$('.dqfx-legend-link').on('click', function () {
				self.requestData();
				var stext = $(this).find('span').text();
				stext = encodeURIComponent(stext);
				var url = 'http://www.iwencai.com/stockpick/search?typed=0&preParams=&ts=1&f=1&qs=client_syqjzf&selfsectsn=&querytype=&searchfilter=&tid=stockpick&w=' + stext;
				commonMethod.openUrlWeb(url, 4);
			});
			$('.dqfx-legend-sh').on('click', function () {
				if (dqfxChart.isShowSh) {
					dqfxChart.isShowSh = false;
				} else {
					dqfxChart.isShowSh = true;
				}
			});

			$('.view_ul_selectbox a').on('click', function () {
				var zdfType = $(this).attr('data-zdftype');
				if (self.param.zf == zdfType || self.param.df == zdfType) {
					return;
				}
				switch (zdfType) {
					case '1' :
						self.param.zf = '1';
						break;
					case '2' :
						self.param.zf = '2';
						break;
					case '3' :
						self.param.zf = '3';
						break;
					case '4' :
						self.param.df = '4';
						break;
					case '5' :
						self.param.df = '5';
						break;
					case '6' :
						self.param.df = '6';
						break;
					default :
						break;
				}
				self.analyticalData();
			});
		}
	};

	module.exports = dqfxView;
});